import {
  Column,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToOne,
  OneToMany,
} from 'typeorm';
import { CommonEntity, CommonStatus } from '~/common/entity/common.entity';
import { TenantEntity } from '../tenant/tenant.entity';
import { MissionUserEntity } from './mission-user.entity';
import { PointsRulesEntity } from '~/modules/points/points-rules/points-rules.entity';
import { MissionType } from './mission.model';

@Entity({ name: 'ah_mission' })
@Index(['id'])
export class MissionEntity extends CommonEntity {
  @Column({ nullable: true, type: 'varchar', length: 255, comment: '任务名称' })
  name: string;

  @Column({ nullable: true, type: 'text', comment: '任务描述' })
  description: string;

  @Column({
    type: 'tinyint',
    width: 1,
    comment: '是否已删除',
    name: 'is_deleted',
    default: CommonStatus.NO,
  })
  isDeleted: number;

  @Column({
    type: 'enum',
    enum: MissionType,
    comment: '任务类型',
    name: 'mission_type',
  })
  missionType: MissionType;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '任务关联数据',
    name: 'mission_related_data',
  })
  missionRelatedData: string;

  @Column({
    nullable: true,
    type: 'datetime',
    comment: '任务开始时间',
    name: 'start_date'
  })
  startDate: Date;

  @Column({
    nullable: true,
    type: 'datetime',
    comment: '任务结束时间',
    name: 'end_date'
  })
  endDate: Date;

  @Column({
    type: 'tinyint',
    width: 1,
    comment: '是否为无限期活动',
    name: 'is_perpetual',
    default: CommonStatus.NO,
  })
  isPerpetual: CommonStatus;

  @ManyToOne(() => TenantEntity, (tenant) => tenant.missions, { nullable: true })
  @JoinTable({ name: 'tenant_id' })
  tenant: TenantEntity;

  @OneToMany(() => MissionUserEntity, (missionUser) => missionUser.mission)
  missionUsers: MissionUserEntity[];

  @ManyToOne(() => PointsRulesEntity, (rule) => rule.missions)
  @JoinColumn({ name: 'points_rule_id' })
  pointsRule: PointsRulesEntity;
}
